package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IHotArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class HotArticleServiceImpl implements IHotArticleService {

    @Autowired
    private IApArticleService articleService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void compute() {
        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();
        // 查询前5天文章
        //从当天0点0分0秒往前推5天
        Date now = new Date();
        Date to = new Date(now.getYear(), now.getMonth(), now.getDate());
        Date from = new Date(to.getTime() - 5 * 24 * 3600 * 1000);
        query.ge(ApArticle::getPublishTime, from);
        query.lt(ApArticle::getPublishTime, to);
        // 过滤已删除已下架的文章
        query.eq(ApArticle::getIsDelete, false);
        query.eq(ApArticle::getIsDown, false);
        List<ApArticle> list = articleService.list(query);
        //计算文章分值
        for (ApArticle article : list) {
            double score = computeScore(article);
            // 可以根据分值排序获取数据 zset支持带分值排序功能
            String key = "first_page_article_0";
            // article中的数据会发生变化，比如点赞\阅读
            //只保存不会变化的数据
            ApArticle toCache = new ApArticle();
            toCache.setId(article.getId());
            toCache.setTitle(article.getTitle());
            toCache.setAuthorName(article.getAuthorName());
            toCache.setAuthorId(article.getAuthorId());
            toCache.setLayout(article.getLayout());
            toCache.setImages(article.getImages());
            toCache.setLabels(article.getLabels());
            toCache.setCreatedTime(article.getCreatedTime());
            toCache.setPublishTime(article.getPublishTime());
            toCache.setStaticUrl(article.getStaticUrl());
            String value = JSON.toJSONString(toCache);
            //数据存入redis
            //为首页首页缓存数据
            redisTemplate.opsForZSet().add(key, value, score);
            // 增加过期时间
            redisTemplate.expire(key, 23 * 60 + 59, TimeUnit.MINUTES);
            //为每个频道首页缓存数据
            String channelKey = "first_page_article_" + article.getChannelId();
            redisTemplate.opsForZSet().add(channelKey, value, score);
            redisTemplate.expire(channelKey, 23 * 60 + 59, TimeUnit.MINUTES);
        }
    }

    /**
     * 计算文章分值
     *
     * @param article
     * @return
     */
    private double computeScore(ApArticle article) {
        double score = 0;
        if (article.getViews() != null) {
            score += 1 * article.getViews();
        }
        if (article.getLikes() != null) {
            score += 3 * article.getLikes();
        }
        if (article.getComment() != null) {
            score += 5 * article.getComment();
        }
        if (article.getCollection() != null) {
            score += 8 * article.getCollection();
        }
        return score;
    }
}
